import UIAbility from '@ohos.app.ability.UIAbility';
import hilog from '@ohos.hilog';
import window from '@ohos.window';
// 引入 Want 类型，给参数 want 指定类型
import Want from '@ohos.app.ability.Want';

// 声明一个参数类型，接受传递来参数需要指定具体的类型，为键值对的形式，key 为字符串类型，value 为数字类型
type AbilityParams = Record<string, number>
export default class HeimaPayAbility extends UIAbility {
  onCreate(want: Want, launchParam) {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
    // 接受 entry 模块中的 entryability 中 ModuleAbilityJump.ets 传递过来的 order_id
    const params = want.parameters as AbilityParams
    // AppStorage 在整个应用设置一个 order_id 的属性，这样所有文件都可以接受到传递过来的参数
    AppStorage.SetOrCreate<number>('order_id', params.order_id)
  }

  onDestroy() {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
  }

  onWindowStageCreate(windowStage: window.WindowStage) {
    // Main window is created, set main page for this ability
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');

    windowStage.loadContent('pages/Index', (err, data) => {
      if (err.code) {
        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
        return;
      }
      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
    });
  }

  onWindowStageDestroy() {
    // Main window is destroyed, release UI related resources
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
  }

  onForeground() {
    // Ability has brought to foreground
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
  }

  onBackground() {
    // Ability has back to background
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
  }
}
